package com.hontech.bv.ui.view.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.hontech.bv.R
import com.hontech.bv.conn.Proto
import com.hontech.bv.http.FirmInfo
import com.hontech.bv.http.queryFirm
import com.hontech.bv.ui.window.showProg

class FirmUpdateAdapter(private val typeId: Int) : RecyclerView.Adapter<FirmRvItem>() {

    private var firms: ArrayList<FirmInfo>? = null

    suspend fun getFirm() {
        val dialog = showProg("获取:${Proto.nameId(typeId)}固件", "正在获取...")
        try {
            firms = queryFirm(typeId)
            notifyDataSetChanged()
            dialog.success("获取成功 ${firms!!.size}个固件")
        } catch (e: Exception) {
            dialog.error(e)
        }
    }

    override fun onBindViewHolder(holder: FirmRvItem, position: Int) {
        holder.setUp(firms!![position])
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FirmRvItem {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.rv_item_firm, parent, false)
        return FirmRvItem(typeId, view)
    }

    override fun getItemCount(): Int {
        return firms?.size ?: 0
    }
}